import java.util.Arrays;

/**
 * 面试题 16.01. 交换数字
 * https://leetcode-cn.com/problems/swap-numbers-lcci/
 */
public class Solutions_mianshi_16_01 {
    public static void main(String[] args) {
        int[] numbers = {1, 2};  // output: {2, 1}
        int[] result = swapNumbers2(numbers);
        System.out.println(Arrays.toString(result));
    }

    /**
     * 加法运算
     */
    public static int[] swapNumbers(int[] numbers) {
        numbers[0] = numbers[0] + numbers[1];
        numbers[1] = numbers[0] - numbers[1];
        numbers[0] = numbers[0] - numbers[1];
        return numbers;
    }

    /**
     * 异或运算
     */
    public static int[] swapNumbers2(int[] numbers) {
        numbers[0] = numbers[0] ^ numbers[1];
        numbers[1] = numbers[0] ^ numbers[1];
        numbers[0] = numbers[0] ^ numbers[1];
        return numbers;
    }

    public static int[] swapNumbers3(int[] numbers) {
        return new int[]{numbers[1], numbers[0]};
    }
}
